package com.demo.entity;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.demo.entity.his.HisPatient;
import com.demo.enums.PatientSexEnum;
import com.demo.enums.PatientStatusEnum;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

/**
 * 病患信息表
 * @author zm
 * @since 2020-11-26
 */
@Data
@TableName("pd_patient")
@ApiModel(value="Patient对象", description="病患信息表")
public class Patient implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "病患序列号")
    @TableId(value = "patient_id", type = IdType.AUTO)
    private Integer patientId;

    @ApiModelProperty(value = "HIS系统病人编号")
    @TableField("his_patient_id")
    private Integer hisPatientId;

    @ApiModelProperty(value = "住院号")
    @TableField("admission_id")
    private String admissionId;

    @ApiModelProperty(value = "院区名称")
    @TableField("hospital_disttrict_name")
    private String hospitalDisttrictName;

    @ApiModelProperty(value = "院区编号")
    @TableField("hospital_disttrict_id")
    private Integer hospitalDisttrictId;

    @ApiModelProperty(value = "类型1内科2外科")
    @TableField("dept_type")
    private Integer deptType;

    @ApiModelProperty(value = "所属科室")
    @TableField("dept_name")
    private String deptName;

    @ApiModelProperty(value = "所属科室编号")
    @TableField("dept_sn")
    @NotNull(message = "所属科室编号不能为空")
    private Integer deptSn;

    @ApiModelProperty(value = "病区号码")
    @TableField("ward_id")
    private Integer wardId;

    @ApiModelProperty(value = "病区名称")
    @TableField("ward_name")
    private String wardName;

    @ApiModelProperty(value = "床位名称")
    @TableField("bed_name")
    private String bedName;

    @ApiModelProperty(value = "床位编号")
    @TableField("bed_id")
    private String bedId;

    @ApiModelProperty(value = "患者姓名")
    @TableField("patient_name")
    private String patientName;

    @ApiModelProperty(value = "患者性别1男2女")
    @TableField("patient_sex")
    private Integer patientSex;

    @ApiModelProperty(value = "出生日期")
    @TableField("patient_birthdate")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date patientBirthdate;

    @ApiModelProperty(value = "入院时间")
    @TableField("admission_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date admissionTime;

    @ApiModelProperty(value = "医疗组长工号")
    @TableField("medical_leader")
    @NotNull(message = "医疗组长工号不能为空")
    private String medicalLeader;

    @ApiModelProperty(value = "医疗组长姓名")
    @TableField("medical_leader_name")
    private String medicalLeaderName;

    @ApiModelProperty(value = "主管医生工号")
    @TableField("doctor_in_charge")
    @NotNull(message = "主管医生工号不能为空")
    private String doctorInCharge;

    @ApiModelProperty(value = "主管医生姓名")
    @TableField("doctor_in_charge_name")
    private String doctorInChargeName;

    @ApiModelProperty(value = "护士长工号")
    @TableField("head_nurse")
    @NotNull(message = "护士长工号不能为空")
    private String headNurse;

    @ApiModelProperty(value = "护士长姓名")
    @TableField("head_nurse_name")
    private String headNurseName;

    @ApiModelProperty(value = "主管护士工号")
    @TableField("nurse_in_charge")
    @NotNull(message = "主管护士工号不能为空")
    private String nurseInCharge;

    @ApiModelProperty(value = "主管护士姓名")
    @TableField("nurse_in_charge_name")
    private String nurseInChargeName;

    @ApiModelProperty(value = "体重")
    @TableField("patient_weight")
    private BigDecimal patientWeight;

    @ApiModelProperty(value = "身高")
    @TableField("patient_height")
    private BigDecimal patientHeight;

    @ApiModelProperty(value = "年龄")
    @TableField("patient_age")
    private Integer patientAge;

    @ApiModelProperty(value = "临床诊断")
    @TableField("diagnosisl")
    @NotBlank(message = "临床诊断不能为空")
    private String diagnosisl;

    @ApiModelProperty(value = "状态 1 新入 2 术后 3 分娩 4 出院")
    @TableField("status")
    private Integer status;

    @ApiModelProperty(value = "出院时间")
    @TableField("discharge_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date dischargeTime;

    @ApiModelProperty(value = "删除时间")
    @TableField("deleted_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date deletedTime;

    @ApiModelProperty(value = "更新时间")
    @TableField("updated_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date updatedTime;

    @TableField(exist = false)
    private Integer vteRisk;

    @TableField(exist = false)
    private Integer bleedingRisk;

    @TableField(exist = false)
    private Integer precaution;

    /**
     * id;
     * admissionId;
     * serialId;
     * wardId;
     * bedName;
     * patientName;
     * patientSex;
     * patientBirthday;
     * admissionTime;
     * diagnosis;
     * medicalLeader;
     * doctorInCharge;
     * headNurse;
     * nurseInCharge;
     * patientWeight;
     * patientHeight;
     * @param hisPatient
     * @return
     */
    public void toPatient(HisPatient hisPatient) {
        if (StringUtils.isNotBlank(hisPatient.getAdmissionId())) {
            this.admissionId = hisPatient.getAdmissionId().trim();
            this.hisPatientId = Integer.parseInt(hisPatient.getAdmissionId().trim());

        }
        this.deptSn = hisPatient.getDeptSn();
        if (StringUtils.isNotBlank(hisPatient.getWardId())) {
            this.wardId = Integer.parseInt(hisPatient.getWardId().trim());
        }
        if (StringUtils.isNotBlank(hisPatient.getBedName())) {
            this.bedName = hisPatient.getBedName().trim();
            this.bedId = hisPatient.getBedName().trim();
        }
        if (StringUtils.isNotBlank(hisPatient.getPatientName())) {
            this.patientName = hisPatient.getPatientName().trim();
        }
        PatientSexEnum sexEnum = PatientSexEnum.getByDesc(StringUtils.trim(hisPatient.getPatientSex()));
        if (sexEnum != null) {
            this.patientSex = sexEnum.getValue();
        }
        if (StringUtils.isNotBlank(hisPatient.getPatientBirthdate())) {
            this.patientBirthdate = DateUtil.parse(hisPatient.getPatientBirthdate().trim(), DatePattern.PURE_DATE_PATTERN);
            this.patientAge = Integer.parseInt(String.valueOf(DateUtil.betweenYear(new Date(), this.patientBirthdate, true)));
        }
        if (hisPatient.getAdmissionTime() != null) {
            this.admissionTime = DateUtil.parse(hisPatient.getAdmissionTime().trim(), "yyyyMMddHH:mm:ss");
        }
        if (StringUtils.isNotBlank(hisPatient.getDiagnosis())) {
            this.diagnosisl = hisPatient.getDiagnosis().trim();
        }
        if (StringUtils.isNotBlank(hisPatient.getMedicalLeader())) {
            this.medicalLeader = hisPatient.getMedicalLeader().trim();
        }
        if (StringUtils.isNotBlank(hisPatient.getDoctorInCharge())) {
            this.doctorInCharge = hisPatient.getDoctorInCharge().trim();
        }
        if (StringUtils.isNotBlank(hisPatient.getHeadNurse())) {
            this.headNurse = hisPatient.getHeadNurse().trim();
        }
        if (StringUtils.isNotBlank(hisPatient.getNurseInCharge())) {
            this.nurseInCharge = hisPatient.getNurseInCharge().trim();
        }
        if (StringUtils.isNotBlank(hisPatient.getPatientWeight())) {
            this.patientWeight = BigDecimal.valueOf(Double.parseDouble(hisPatient.getPatientWeight().trim()));
        }
        if (StringUtils.isNotBlank(hisPatient.getPatientHeight())) {
            this.patientHeight = BigDecimal.valueOf(Double.parseDouble(hisPatient.getPatientHeight().trim()));
        }
        this.status = PatientStatusEnum.XIN_RU.getValue();
    }
}
